﻿using System.Collections.Generic;
using System.Collections;
using System;
using UnityEngine;
using LitJson;

public class EventManager : MonoBehaviour
{
    public static EventManager Instance
    {
        get
        {
            if (_Instance == null)
            {
                _Instance = FindObjectOfType<EventManager>();
            }
            if (!_Instance)
            {
                GameObject obj = new GameObject("EventManager");
                _Instance = obj.AddComponent<EventManager>();
            }
            return _Instance;
        }
        set
        {
            _Instance = value;
        }
    }
    private List<int> priorities = new List<int>();
    private static EventManager _Instance;
    // Start is called before the first frame update
    EvenInfoList eventInfoList;
    Dictionary<int, EventInfo> eventDict;
    private int maxEventNum;//基础事件数
    void Start()
    {
        ParseEventInfoList();
    }

    private void ParseEventInfoList()
    {
        maxEventNum = 0;
        eventDict = new Dictionary<int, EventInfo>();
        TextAsset textEventInfoList = Resources.Load<TextAsset>("Data/EventInfoList");
        //Debug.Log(textEventInfoList);
        eventInfoList = JsonMapper.ToObject<EvenInfoList>(textEventInfoList.text);
        foreach(EventInfo eventInfo in eventInfoList.eventInfoList)
        {
            eventDict.Add(eventInfo.id, eventInfo);
            if(eventInfo.id<10000&& eventInfo.id> maxEventNum)
            {
                maxEventNum = eventInfo.id;
            }
        }
        priorities.Add(0);
        for(int i=1;i<=maxEventNum;i++)
        {
            priorities.Add(priorities[i-1]+ eventDict.GetValue(i).priority);
            //Debug.Log(i + " : " + priorities[i]);
        }
    }

    public int getRandomEvent()
    {
        if (RandomGenerator.Instance.Next(ConstOfMap.EVENTRATE) == 0)
        {
            int rate= RandomGenerator.Instance.Next(priorities[maxEventNum] + 1);
            for (int i= 1; i<= maxEventNum; i++)
            {
                if (rate < priorities[i])
                {
                    return i;
                }
            }
        }
        return 0;
    }

    public void OnTriggerEvent(int id)//触发事件
    {
        EventPanel ep;
        if (id == 0)//没有event
        {
            //Debug.Log("ccccccccccccccc");
            return;
        }
        if (eventDict.GetValue(id) == null)//找不到event
        {
            Debug.LogError("找不到event，id=" + id);
        }
        switch(eventDict.GetValue(id).optionNum)
        {
            case 1:
                ep = (EventPanel)UIManager.Instance.GetPanel("OneEventPanel");
                ep.setEvent(eventDict.GetValue(id));
                UIManager.Instance.PushPanel("OneEventPanel");
                break;
            case 2:
                ep = (EventPanel)UIManager.Instance.GetPanel("DouEventPanel");
                ep.setEvent(eventDict.GetValue(id));
                UIManager.Instance.PushPanel("DouEventPanel");
                break;
            case 3:
                ep = (EventPanel)UIManager.Instance.GetPanel("TriEventPanel");
                ep.setEvent(eventDict.GetValue(id));
                UIManager.Instance.PushPanel("TriEventPanel");
                break;
        }
    }

    public int getEventNum()
    {
        return maxEventNum;
    }
}
